//
//  SCTextView.m
//  Shuaiche
//
//  Created by dangshuai on 2019/4/26.
//  Copyright © 2019 zzs. All rights reserved.
//

#import "SCTextView.h"
#import <Masonry.h>
#import "SCHelper.h"
#import "UIView+InputHandler.h"
@interface SCTextView ()<UITextViewDelegate>
@property (nonatomic, strong) UILabel *limitLabel;
@property (nonatomic, strong) UILabel *placeholderLabel;

@property (nonatomic, assign) BOOL hasBeyondLimit;
@end

@implementation SCTextView

- (void)setLimitLength:(NSInteger)limitLength {
    _limitLength = limitLength;
    if (limitLength > 0) {
        self.limitLabel.text = [NSString stringWithFormat:@"0/%zd",limitLength];
        [_textView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.mas_bottom).offset(-20);
        }];
    }
}

- (void)setPlaceHolder:(NSString *)placeHolder {
    _placeHolder = placeHolder;
    if (_placeHolder.length) {
        self.placeholderLabel.text = _placeHolder;
    }
}

- (void)setText:(NSString *)text {
    self.textView.text = text;
    if (_limitLength > 0) {
        self.limitLabel.text = [NSString stringWithFormat:@"%zd/%zd",text.length,_limitLength];
        [_textView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.mas_bottom).offset(-20);
        }];
    }
}

- (NSString *)text {
    return _textView.text;
}

-  (UILabel *)limitLabel {
    if (!_limitLabel) {
        _limitLabel = [[UILabel alloc] init];
        _limitLabel.font = SC_FONT(10);
        _limitLabel.textColor = SC_TextBlack_Third;
        [self addSubview:_limitLabel];
        [_limitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(@(-10));
            make.bottom.equalTo(@(-5));
        }];
    }
    return _limitLabel;
}

- (UILabel *)placeholderLabel {
    if (!_placeholderLabel) {
        _placeholderLabel = [[UILabel alloc] init];
        _placeholderLabel.font = SC_FONT(14);
        _placeholderLabel.textColor = SC_TextBlack_Third;
        _placeholderLabel.numberOfLines = 0;
        [self.textView addSubview:_placeholderLabel];
        [_textView setValue:_placeholderLabel forKey:@"_placeholderLabel"];
    }
    return _placeholderLabel;
}

- (UITextView *)textView {
    if (!_textView) {
        _textView = [[UITextView alloc] init];
        _textView.backgroundColor = [UIColor clearColor];
        _textView.font = SC_FONT(14);
        _textView.textColor = SC_TextBlack_Primary;
        _textView.textContainerInset = UIEdgeInsetsZero;
        _textView.delegate = self;
        _textView.scEmojiUnEnabled = YES;
        [self addSubview:_textView];
        CGFloat b = _limitLength == 0 ? 12 : 20;
        [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@5);
            make.top.equalTo(@12);
            make.right.equalTo(@(-10));
            make.bottom.equalTo(self.mas_bottom).offset(-b);
        }];
    }
    return _textView;
}

#pragma mark -- UITextViewDelegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if ([SCHelper stringContainsEmoji:text]) {
        return NO;
    }
    
    if (text.length == 0) {
        return YES;
    }
    
    if (_hasBeyondLimit) {
        return NO;
    }
    
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
    
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
    if (selectedRange && pos) {
        return;
    }
    
    NSInteger limit = _limitLength > 0 ? _limitLength : NSIntegerMax;
    
    _hasBeyondLimit = textView.text.length >= limit;
    if (textView.text.length > limit) {
        textView.text = [textView.text substringToIndex:limit];
    }
    _limitLabel.text = [NSString stringWithFormat:@"%zd/%zd",textView.text.length,limit];
    
    if ([_delegate respondsToSelector:@selector(scTextViewDidChanged:)]) {
        [_delegate scTextViewDidChanged:textView];
    }
}

@end
